<template>
	<view class="container-withdrawal">
		<view class="app">
			<view class="cell1">
				<view class="title">
					{{$t('legumes.name1')}}{{titleName}}
				</view>
				<view class="input flex-between">
					<input :placeholder="$t('legumes.name2')" border="surround" v-model="money" type="number" @input="handleInput"
						@change="change" placeholder-class="placeholder">
					<text style="font-size: 24rpx;color: #1E84F1;" @tap="handleMoney">{{$t('legumes.name10')}}</text>
					</input>
				</view>
				<view class="balance">
					{{$t('legumes.name3')}}： {{balance}}
				</view>
			</view>
			<view class="switch">
				<image src="@/static/user/switch.png" mode=""></image>
			</view>
			<view class="cell1">
				<view class="title">
					<view class="type flex" @tap="show = true">
						<text>{{$t('legumes.name4')}} {{ typeText || $t('legumes.name5') }}</text><text v-if="toprice">({{toprice}})</text>
						<u-icon name="arrow-down-fill"  size="14"></u-icon>
					</view>
				</view>
				<view class="input">
					<input :placeholder="$t('legumes.name2')" border="surround" v-model="converInfo.num" @change="change" 
						placeholder-class="placeholder" disabled></input>
				</view>
				<view class="balance">
					{{$t('legumes.name6')}}：{{converInfo.sxf}}
				</view>
			</view>
			<view class="btn" style="margin: 100rpx 0 50rpx;">
				<view class="conversion">
					{{converInfo.price}}
				</view>
				<u-button type="primary" :loading="btnloading" :text="$t('legumes.name7')" color="#1e84f1" shape="circle"
					@click="confirm" :disabled="disabled" ></u-button>
			</view>
		</view>
		<u-action-sheet :actions="list" :title="$t('legumes.name8')" :show="show" :cancelText="$t('cancel')" @close="show = false" round="20"
			@select="handleSelect" safeAreaInsetBottom></u-action-sheet>
		<pay-board ref="payJP" :showPrice='false' :showBoard="showBoard" @complete="complete"
			@closeBorad="showBoard = false"></pay-board>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				showPwd: false,
				money: '',
				list: [],
				bankList: [],
				bid: '',
				id: '',
				titleName: '', //标题
				placeholder: '',
				active: "`",
				balance: '==',
				withdrawal_type: 0,
				isChecked: false,
				btnloading: false,
				marthlist: [],
				show: false,
				typeText: '',
				typeParams: {},
				list: [],
				showBoard: false,
				converInfo:{},
				price:0,
				toprice:0
			}
		},
		// onNavigationBarButtonTap(e) {
		// 	this.$fun.jump(`/pages/user/wallet/walletdetail?id=${this.id}`)
		// },
		onReady() {
			uni.setNavigationBarTitle({
				title:this.$t('wallet.name2')
			})
		},
		onLoad(options) {
			this.id = options.id
			this.titleName = options.name
			this.bid = options.bid
			// this.$fun.ajax.post('index/config', {}).then(res => {
			// 	this.placeholder = res.data.withdrawal_title
			// 	this.withdrawal_type = res.data.withdrawal_type
			// })
			this.getList()
			this.convertCoinList()
		},
		computed: {
			disabled() {
				if(this.money && this.typeParams.id){
					return false
				}else{
				return true
				}
			}
		},
		watch: {
			money(val) {
				if (val) {
					this.money1 = ''
				}
			}
		},
		methods: {
			// 获取换算汇率
			async handleInput() {
				if(!this.typeParams.id) return
				if(this.money){
					const data = {
						money:this.money,
						bWalletId: this.id,
						hWalletId: this.typeParams.id
					}
					const res = await this.$fun.ajax.post('user/convertHs', data)
					this.converInfo = res.data
				}else{
					this.converInfo = {}
				}
				
			},
			handleSelect(e) {
				this.typeText = e.name
				this.typeParams = e
				this.toprice=e.price
				this.handleInput()
			},
			handleMoney() {
				if (this.balance <= 0) return this.$fun.msg(this.$t('wallet.name14'))
				this.money = this.balance
				this.handleInput()
			},
			async convertCoinList() {
				const res = await this.$fun.ajax.post('user/convertCoinList', {
					id: this.id,
				})
				if (res.code == 1) {
					this.balance = res.data.balance
					this.price=res.data.price
					this.list = res.data.wallet
				}
			},
			async getList() {
				const res = await this.$fun.ajax.post('user/withdrawalList', {
					id: this.id,
					page: 1,
					limit: 10
				})
				if (res.code == 1) {
					this.marthlist = res.data
				}

			},
			onChange(item, index) {
				// //console.log(e)
				this.active = index;
				this.bid = item.id;
			},
			configBank(e) {
				this.bid = this.bankList[e.index].id
			},
			async confirm() {
				if (this.btnloading) return this.$fun.msg('...')
				let {
					id,
					money,
					typeParams
				} = this;
				let parms = {
					money,
					bWalletId: id,
					hWalletId: typeParams.id
				}
				if (!typeParams.id) return this.$fun.msg(this.$t('legumes.name9'))
				if (!money) return this.$fun.msg(this.$t('legumes.name2'))
				if (+money <= 0) return this.$fun.msg(this.$t('wallet.name14'))
				if (+money > +this.balance) return this.$fun.msg(this.$t('wallet.name14'))
				this.showBoard = true
			},
			complete(pass) {
				this.$fun.ajax.post('user/setPay', {
					paypwd: pass
				}).then(res => {
					this.$refs.payJP.password = ''
					if (res.code == 1) {
						this.showBoard = false
						this.upload()
					}
				})
			},
			async upload() {
				let {
					id,
					money,
					typeParams
				} = this;
				let parms = {
					money,
					bWalletId: id,
					hWalletId: typeParams.id
				}
				await this.$fun.ajax.post('user/convert', {
					...parms
				}).then(res => {
					if (res.code == 1) {
						this.$fun.msg(res.msg)
						this.$fun.jump('', 4, 1200)
					}
				}).catch(() => {})
			},
			changePwdState() {
				this.showPwd = !this.showPwd;
			}
		},
		onNavigationBarButtonTap(e) {
			const index = e.index;
			// #ifdef APP-PLUS
			const pages = getCurrentPages();
			const page = pages[pages.length - 1];
			const currentWebview = page.$getAppWebview();
			currentWebview.hideTitleNViewButtonRedDot({
				index
			});
			// #endif
			uni.navigateTo({
				url: `./withdrawList?id=${this.id}&name=${this.titleName}`
			})
		}

	}
</script>

<style scoped lang="scss">
	page {
		height: 100%;
		background: #F2F2F2;
	}

	.container-withdrawal {
		height: 100%;

		.records {
			margin: 20rpx 0;

			.header {
				.lt {
					font-family: PingFang SC, PingFang SC;
					font-weight: bold;
					font-size: 28rpx;
					color: #232323;
					border-left: 5rpx solid #1e84f1;
					padding-left: 10rpx;
				}

				.rt {
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					font-size: 24rpx;
					color: #999999;
				}
			}

			.content {
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				font-size: 24rpx;
				color: #232323;
				line-height: 28rpx;

				.item {
					border-bottom: 1rpx solid #dedede;
					padding: 5px 0;
				}

				.price {
					font-family: DIN, DIN;
					font-weight: bold;
					font-size: 24rpx;
					color: #232323;
				}

				.createTime {
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					font-size: 24rpx;
					color: #999999;
					line-height: 32rpx;
				}
			}
		}
	}

	.app {
		position: absolute;
		top: 50rpx;
		left: 50%;
		transform: translate(-50%);
		// background-color: #fff;
		padding: 10rpx 30rpx 30rpx;
		border-radius: 20rpx;
		height: 70%;
		width: 95%;

		.add-btn {
			margin: 0 30rpx;
		}

		// padding: 20rpx;
		.top-box {
			color: #232323;
			padding: 20rpx 20rpx 50rpx;

			.rule {
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				font-size: 24rpx;
				color: #232323;
				text-align: end;
			}

			.money-box {
				text-align: center;

				.money-name {
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					font-size: 24rpx;
					color: #232323;
				}

				.money {
					font-family: DIN, DIN;
					font-weight: bold;
					font-size: 64rpx;
					color: #232323;

					text {
						font-size: 40rpx;
					}
				}
			}
		}

		/* 内容区 */
		.conter {
			.title {
				color: #666;
				margin-top: 50rpx;
				font-size: 30rpx;
			}

			.cent {
				margin-top: 25rpx;
				overflow: hidden;

				.list {
					width: 200rpx;
					height: 120rpx;
					border: 1rpx solid #e8e8e8;
					float: left;
					margin-right: 39rpx;
					margin-bottom: 45rpx;
					position: relative;
					text-align: center;
					line-height: 120rpx;
					border-radius: 15rpx;

					&:nth-child(3n) {
						margin-right: 0;
					}

					input {
						height: 50rpx;
						margin-left: 10rpx;
						margin-top: 35rpx;
					}

					.van-cell {
						padding: 0;
						width: 180rpx;
					}

					image {
						width: 40rpx;
						height: 37rpx;
						position: absolute;
						bottom: 0;
						right: 0;
						display: none;
					}

					&.active {
						color: #dd1021;
						border: 1rpx solid #dd1021;

						image {
							display: block;
						}
					}
				}
			}
		}

		.bottom {
			width: 690rpx;

			.text {
				margin-bottom: 30rpx;
				display: flex;
				font-size: 24rpx;
				line-height: 40rpx;

				.van-checkbox {}

				navigator {
					color: #dd1021;
				}
			}

			button {
				width: 690rpx;
				height: 78rpx;
				line-height: 78rpx;
				background: #dd1021;
				color: #fff;
				border-radius: 78rpx;
				font-weight: 500;
				margin: 0;
				padding: 0;
			}
		}
	}

	/* 提现 */
	.member {
		.conter {
			width: 694rpx;
			height: 175rpx;
			background: #f3f3f3;
			margin: 20rpx auto;
			border-radius: 10rpx;
			display: flex;

			&.active {
				border: 1px solid #1E84F1;
			}

			.left {
				margin-left: 20rpx;
				margin-top: 25rpx;
				width: 425rpx;

				.top {
					width: 82rpx;
					height: 30rpx;
					font-size: 18rpx;
					color: #fff;
					background: linear-gradient(to right, #242a42, #474e61);
					border-radius: 30rpx;

					image {
						width: 20rpx;
						height: 17rpx;
						margin-left: 10rpx;
						margin-right: 5rpx
					}
				}

				.cent {
					color: #333;
					font-size: 32rpx;
					margin-top: 10rpx;

					text {
						color: #1E84F1;
					}
				}

				.name {
					margin-top: 15rpx;
					font-size: 24rpx;
					color: #666;
				}
			}

			.data {
				margin-top: 65rpx;
				color: #000;
				font-size: 28rpx;
				margin-right: 20rpx;
			}

			.van-checkbox {
				margin-top: 65rpx;
			}
		}
	}



	.nocell {
		border-bottom: none !important;
	}
	.switch{
		width: 37rpx;
		height: 42rpx;
		margin: 35rpx auto;
	}

	.btn {
		.conversion {
			font-family: PingFangSC, PingFang SC;
			font-weight: 500;
			font-size: 28rpx;
			color: #333333;
			line-height: 28rpx;
			text-align: center;
			font-style: normal;
			margin: 30rpx 0;
			
		}
	}

	.cell1 {
		.title {
			font-family: PingFangSC, PingFang SC;
			font-weight: 500;
			font-size: 28rpx;
			color: #333333;
			line-height: 28rpx;
			text-align: left;
			font-style: normal;
		}

		.input {
			height: 80rpx;
			background: #FFFFFF;
			border-radius: 24rpx;
			display: flex;
			align-items: center;
			padding: 0 20rpx;
			margin: 20rpx 0;

			.placeholder {
				font-family: PingFangSC, PingFang SC;
				font-weight: 400;
				font-size: 28rpx;
				color: #CCCCCC;
				line-height: 28rpx;
				text-align: left;
				font-style: normal;
			}
		}

		.balance {
			font-family: PingFangSC, PingFang SC;
			font-weight: 400;
			font-size: 24rpx;
			color: #999999;
			line-height: 24rpx;
			text-align: left;
			font-style: normal;
		}

	}

	.cell {
		display: flex;
		align-items: center;
		min-height: 110rpx;
		margin: 0 40rpx;
		border-bottom: 1px solid rgb(236, 236, 236);

		&:first-child {
			margin-bottom: 10rpx;
		}

		&:after {
			left: 40rpx;
			right: 40rpx;
			border-color: #d8d8d8;
		}

		.tit {
			font-size: 30rpx;
			color: #333;
		}

		.avatar-wrap {
			width: 120rpx;
			height: 120rpx;
			position: relative;
			border-radius: 100rpx;
			overflow: hidden;

			.avatar {
				width: 100%;
				height: 100%;
				border-radius: 100rpx;
			}

			.progress {
				position: absolute;
				left: 50%;
				top: 50%;
				transform: translate(-50%, -50%);
				width: 100rpx;
				height: 100rpx;
				box-shadow: rgba(0, 0, 0, .6) 0px 0px 0px 2005px;
				border-radius: 100rpx;
				transition: .5s;
				opacity: 0;

				&.no-transtion {
					transition: 0s;
				}

				&.show {
					opacity: 1;
				}
			}
		}

		.input {
			flex: 1;
			text-align: right;
			font-size: 28rpx;
			color: #333;
		}

		switch {
			margin: 0;
			transform: scale(0.8) translateX(10rpx);
			transform-origin: center right;
		}

		.tip {
			margin-left: 20rpx;
			font-size: 28rpx;
			color: #999;
		}

		.checkbox {
			padding: 12rpx 0 12rpx 40rpx;
			font-size: 28rpx;
			color: #333;

			.mix-icon {
				margin-right: 12rpx;
				font-size: 36rpx;
				color: #ccc;
			}

			.icon-xuanzhong {
				color: $base-color;
			}
		}
	}
</style>